package cn.sirun.com.fca.account.uitl;

import android.app.Activity;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.utils.DialogUtils;

import java.util.HashMap;
import java.util.Map;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountStatusCode;

/**
 * @author vinsonhuang
 * @date 2018/10/29
 */
public class AccountStatusUtils {

    private static Map<Integer, String> statusMap = new HashMap<>();

    /**
     * code --> message 映射表
     *
     * 注：不需要弹窗或者弹窗信息需要定制的，message赋值为空串
     */
    static {
        statusMap.put(AccountStatusCode.LOGIN_COOKIE_NON_EXISTENT, "");
        statusMap.put(AccountStatusCode.NOT_PERMISSION,
                AccountStatusCode.MSG_NOT_PERMISSION);
        statusMap.put(AccountStatusCode.ACCESS_SESSION_ID_INVALID, "");
        statusMap.put(AccountStatusCode.PICTURE_RECOGNIZE_FAILED,
                AccountStatusCode.MSG_PICTURE_RECOGNIZE_FAILED);
        statusMap.put(AccountStatusCode.LOGIN_STATUS_VERIFY_FAILED, "");
        statusMap.put(AccountStatusCode.NEED_CAPTCHA,
                AccountStatusCode.MSG_NEED_CAPTCHA);
        statusMap.put(AccountStatusCode.VEHICLE_IS_BOUND,
                AccountStatusCode.MSG_VEHICLE_IS_BOUND);
        statusMap.put(AccountStatusCode.INVALID_IDENTIFY_INFO,
                AccountStatusCode.MSG_INVALID_IDENTIFY_INFO);
        statusMap.put(AccountStatusCode.NEED_VERIFY, "");
        statusMap.put(AccountStatusCode.ACCOUNT_SESSION_ID_INVALID, "");
        statusMap.put(AccountStatusCode.SMS_RESEND_FREQUENTLY,
                AccountStatusCode.MSG_SMS_RESEND_FREQUENTLY);
        statusMap.put(AccountStatusCode.SMS_CAPTCHA_ERROR,
                AccountStatusCode.MSG_SMS_CAPTCHA_ERROR);
        statusMap.put(AccountStatusCode.SMS_CAPTCHA_MULTI_ERROR,
                AccountStatusCode.MSG_SMS_CAPTCHA_MULTI_ERROR);
        statusMap.put(AccountStatusCode.FLOW_ERROR,
                AccountStatusCode.MSG_FLOW_ERROR);
        statusMap.put(AccountStatusCode.HAS_REGISTER,
                AccountStatusCode.MSG_HAS_REGISTER);
        statusMap.put(AccountStatusCode.SYSTEM_ERROR, "");
        statusMap.put(AccountStatusCode.UNREGISTERED, "");
        statusMap.put(AccountStatusCode.SEND_SMS_FAILED,
                AccountStatusCode.MSG_SEND_SMS_FAILED);
        statusMap.put(AccountStatusCode.CAPTCHA_VERIFY_FAILED,
                AccountStatusCode.MSG_CAPTCHA_VERIFY_FAILED);
        statusMap.put(AccountStatusCode.USERNAME_OR_PWD_ERROR,
                AccountStatusCode.MSG_USERNAME_OR_PWD_ERROR);
        statusMap.put(AccountStatusCode.INVALID_PARAMETERS,
                AccountStatusCode.MSG_INVALID_PARAMETERS);
        statusMap.put(AccountStatusCode.LOGIN_STATUS_INVALID,
                AccountStatusCode.MSG_LOGIN_STATUS_INVALID);
        statusMap.put(AccountStatusCode.URL_INVALID,
                AccountStatusCode.MSG_URL_INVALID);
        statusMap.put(AccountStatusCode.PROFILE_ERROR,
                AccountStatusCode.MSG_PROFILE_ERROR);
        statusMap.put(AccountStatusCode.ID_INFO_INVALID,
                AccountStatusCode.MSG_ID_INFO_INVALID);
        statusMap.put(AccountStatusCode.NEED_VERIFY_PIN, "");
    }

    public static String getStatusMessage(int code) {
        return statusMap.get(code);
    }

    public static void showToast(Activity activity, int code) {
        showToast(activity, code, R.string.common_request_failed);
    }

    public static void showToast(Activity activity, int code, @StringRes int defaultMessage) {
        showToast(activity, code, ResourcesUtils.getString(defaultMessage));
    }

    public static void showToast(Activity activity, int code, String defaultMessage) {
        if (null == activity) {
            return;
        }
        String message = getStatusMessage(code);
        if (!TextUtils.isEmpty(message)) {
            DialogUtils.showToast(activity, message);
        } else {
            DialogUtils.showToast(activity, defaultMessage);
        }
    }

}
